package com.ucode.application.controller.sys;

import java.util.List;

import javax.validation.constraints.Size;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ucode.application.form.sys.MenuForm;
import com.ucode.sys.model.Menu;
import com.ucode.sys.service.MenuService;
import com.ucode.sys.vo.MenuTree;
import com.ucode.tool.base.Paginator;
import com.ucode.tool.base.ResponseResult;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags="系统菜单管理")
@RestController
@RequestMapping("/sys-menu")
@Validated
public class MenuController {

    @Autowired
    private MenuService menuService;
    
    @PreAuthorize("hasAuthority('sys:menu:read') or hasRole('ADMIN')")
    @ApiOperation("根据条件分页查询列表")
    @GetMapping(value = "/list")
    public ResponseResult<Object> findPage(
            @ApiParam(value ="所属子系统")  @RequestParam(value = "subSys",required = false) Integer subSys,
            @ApiParam(value ="父节点")  @RequestParam(value = "pid",required = false) Long pid,
            @ApiParam(value ="菜单名称")  @RequestParam(value = "name",required = false) String name,
            @ApiParam(value ="权限值")  @RequestParam(value = "permission",required = false) String permission,
            @ApiParam(value ="菜单类型")  @RequestParam(value = "menuType",required = false) Integer menuType,
            @ApiParam(value="页码",example="1") @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @ApiParam(value="每页记录数",example="5") @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        
        if(StrUtil.isNotBlank(name))
            name = StrUtil.concat(true, name,"%");
        
        if(StrUtil.isNotBlank(permission))
            permission = StrUtil.concat(true,permission, "%");
        
        Paginator<Menu> paginator = menuService.queryPage(subSys,pid, name, permission, menuType, pageNum, pageSize);
        
        return ResponseResult.success(paginator);
    }
    
    @PreAuthorize("hasAuthority('sys:menu:read') or hasRole('ADMIN')")
    @ApiOperation(value="查询树形结构菜单")
    @ApiImplicitParam(name = "subSys", value = "所属子系统", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getMenus/{subSys}")
    public ResponseResult<Object> getMenus(@PathVariable(name="subSys",required=true) Integer subSys){
        List<MenuTree> menus = menuService.findMenuTreeList(subSys);
        return ResponseResult.success(menus);
    }
    
    @PreAuthorize("hasAuthority('sys:menu:read') or hasRole('ADMIN')")
    @ApiOperation(value="根据主键查询")
    @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "String",paramType = "path")
    @GetMapping("/getById/{id}")
    public ResponseResult<Object> getById(@PathVariable(name="id",required=true) Long id){
        Menu menu = menuService.findById(id);
        return ResponseResult.success(menu);
    }
    
    @PreAuthorize("hasAuthority('sys:menu:write') or hasRole('ADMIN')")
    @ApiOperation(value="创建")
    @PostMapping("/create")
    public ResponseResult<Object> create(@Validated @RequestBody MenuForm form) {
        
        Menu menu = new Menu(form.getPid(), form.getSubSys(),form.getName(),form.getPermission(), form.getMenuType(),  form.getRemark());
        menu = menuService.insert(menu);
        return menu != null ? ResponseResult.success(menu) : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:menu:write') or hasRole('ADMIN')")
    @ApiOperation(value="更新")
    @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/update/{id}")
    public ResponseResult<Object> update(@PathVariable(name="id",required=true) Long id,
            @Validated @RequestBody MenuForm form) {
        
        
        Menu menu = new Menu(form.getPid(),form.getSubSys(), form.getName(),form.getPermission(), form.getMenuType(), form.getRemark());
        menu.setId(id);
        int i = menuService.update(menu);
        
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:menu:write') or hasRole('ADMIN')")
    @ApiOperation(value="删除")
    @ApiImplicitParam(name = "id", value = "菜单ID", required = true, dataType = "String",paramType = "path")
    @PostMapping("/delete/{id}")
    public ResponseResult<Object> delete(@PathVariable(name="id",required=true) Long id) {
        int i = menuService.deleteById(id);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    @PreAuthorize("hasAuthority('sys:menu:write') or hasRole('ADMIN')")
    @ApiOperation(value="批量删除")
    @PostMapping("/delete")
    public ResponseResult<Object> delete(
            @ApiParam(value ="菜单ID(最多100条)")  @Size(min=1,max = 100,message="一次最多选择100条")@RequestParam(value = "ids") List<Long> ids) {
        int i = menuService.delete(ids);
        return i > 0 ? ResponseResult.success() : ResponseResult.failed();
    }
    
    
}
